//
//  HZCartTotalPanel.m
//  app
//
//  Created by 刘华舟 on 15/5/4.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZCartTotalPanel.h"
//constant
#import "HZImageSourceConstant.h"
//model
#import "HZCartTotalModel.h"


static const CGFloat kCartTotalPanelHeight = 90.0f;
static const CGFloat kCartTotalSelectAllBtnWidth = 64.0f;
static const CGFloat kCartTotalSelectAllLabelWidth = 60.0f;
static const CGFloat kCartTotalGoodsNumBtnWidth = 180.0f;
static const CGFloat kCartTotalMoneyPading = 10.0f;



@interface HZCartTotalPanel()

@property (strong, nonatomic) UIButton *selectAllBtn;
@property (strong, nonatomic) UILabel *selectAllLabel;
@property (strong, nonatomic) UILabel *totalMoneyLabel;
@property (strong, nonatomic) UIButton *totalGoodsNumBtn;

@end

@implementation HZCartTotalPanel

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
        self.alpha = HZ_MALL_LIGHTGRAY_BACKGROUND_ALPHA;
        //定义面板的高度
        self.ubo.height = [self.ubo uh:kCartTotalPanelHeight];
        
        //初始化全选按钮
        self.selectAllBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                                     [self.ubo uw:kCartTotalSelectAllBtnWidth fh:UB_HEIGHT])];
        [self.selectAllBtn setImage:[UIImage imageNamed:kImageMallCartGoodUnselect] forState:UIControlStateNormal];
        [self.selectAllBtn setImage:[UIImage imageNamed:kImageMallCartGoodSelected] forState:UIControlStateSelected];
        self.selectAllBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        self.selectAllBtn.backgroundColor = HZ_CLEAR_COLOR;
        [self.selectAllBtn addTarget:self action:@selector(selectAllBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.selectAllBtn];
        
        //初始化全选标签
        self.selectAllLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.selectAllBtn.ufo fx:UB_RIGHT ux:kCartTotalMoneyPading fy:UB_TOP],
                                                                      [self.selectAllBtn.ubo uw:kCartTotalSelectAllLabelWidth fh:UB_HEIGHT])];
        self.selectAllLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.selectAllLabel.textAlignment = NSTextAlignmentLeft;
        self.selectAllLabel.font = JKAppGlobleFontContentText;
        [self addSubview:self.selectAllLabel];
        
        //初始化商品个数按钮
        self.totalGoodsNumBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_RIGHT ux:-(kCartTotalGoodsNumBtnWidth) fy:UB_TOP],
                                                                         [self.ubo uw:kCartTotalGoodsNumBtnWidth fh:UB_HEIGHT ph:-2])];
        [self.totalGoodsNumBtn setBackgroundColor:HZ_MALL_MAIN_THEME_RED];
        [self.totalGoodsNumBtn setTitleColor:HZ_WHITE_COLOR forState:UIControlStateNormal];
        [self.totalGoodsNumBtn.titleLabel setFont:JKAppGlobleFontContentText];
        [self.totalGoodsNumBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
//        self.totalGoodsNumBtn.layer.masksToBounds = YES;
//        self.totalGoodsNumBtn.layer.cornerRadius = [self.totalGoodsNumBtn.ubo uh:4];
        [self.totalGoodsNumBtn addTarget:self action:@selector(totalGoodsBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.totalGoodsNumBtn];
        
        //初始化合计金钱总额
        CGFloat totalMoneyLabelWidht = [self.ubo fw:UB_WIDTH uw:-(kCartTotalSelectAllBtnWidth + kCartTotalSelectAllLabelWidth + kCartTotalGoodsNumBtnWidth + 2*kCartTotalMoneyPading)] ;
        self.totalMoneyLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.selectAllLabel.ufo fx:UB_RIGHT fy:UB_TOP],
                                                                      [self.ubo ow:totalMoneyLabelWidht fh:UB_HEIGHT])];
        self.totalMoneyLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.totalMoneyLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:self.totalMoneyLabel];
        
        
    }
    return self;
}

- (void)setModel:(HZCartTotalModel *)model{
    _model = model;
    
    self.selectAllBtn.selected = self.model.isAllSelected;
    
    self.selectAllLabel.text = @"全选";
    
    NSMutableAttributedString *atbTotalMoney = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"合计：￥%@", model.totalMoney] attributes:nil];
    [atbTotalMoney addAttribute:NSForegroundColorAttributeName value:HZ_MALL_TEXT_GRAY_COLOR range:NSMakeRange(0, 3)];
    [atbTotalMoney addAttribute:NSForegroundColorAttributeName value:HZ_RED_COLOR range:NSMakeRange(3, atbTotalMoney.length -3)];
    [atbTotalMoney addAttribute:NSFontAttributeName value:JKAppGlobleFontSmallTagText range:NSMakeRange(0, atbTotalMoney.length)];
    self.totalMoneyLabel.attributedText = atbTotalMoney;
    
    [self.totalGoodsNumBtn setTitle:[NSString stringWithFormat:@"去结算(%@)", model.totalGoodsNum] forState:UIControlStateNormal];
    
    
}

//全选
- (void)selectAllBtnOnClick{
    
    HZLog(@"点击全选按钮");
    
    self.model.allSelected = !self.model.isAllSelected;
    self.selectAllBtn.selected = self.model.isAllSelected;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(cartTotalPanelSelectAllWithPanel:)]) {
        [self.delegate cartTotalPanelSelectAllWithPanel:self];
    }
}

//去结算
- (void)totalGoodsBtnOnClick{
    HZLog(@"点击结算按钮");
    
    if ([SNTool shouldLoginFromController:self]) {
        return;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(cartTotalPanelCloseAccountWithPanel:)]) {
        [self.delegate cartTotalPanelCloseAccountWithPanel:self];
        return;
    }

}

@end
